package m1;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.Scanner;

/**
 * @author Amos
 * @date 2022/4/27
 */

public class Producer {
    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        // 创建生产者实例
        DefaultMQProducer p = new DefaultMQProducer("p1");

        // 设置 name server 地址
        p.setNamesrvAddr("192.168.126.133:9876");

        // 启动(连接)
        p.start();

        // 用 Message 封装消息、发送的位置
        /*
            Topic 一级分类
            Tag 二级分类(可选)
         */
        while (true) {
            System.out.print("输入消息：");
            String s = new Scanner(System.in).nextLine();
            Message msg = new Message("Topic1", "Tag1", s.getBytes());
            // 设置延时级别，倒计时10秒
            msg.setDelayTimeLevel(3);
            SendResult result = p.send(msg);
            System.out.println(result);
        }
        // 发送消息
    }
}
